<?php
class BranchesController extends AppController {

	var $name = 'Branches';

	function admin_index() {
		$this->Branch->recursive = 0;
		//var_dump($this->params);
		//$this->params['form']['filter_city'];
		$city_id = !empty($this->params['form']['filter_city']) ? $this->params['form']['filter_city'] : 0;
		if($city_id)
		{
			$this->paginate = array(
				'conditions' => array('city_id' => $city_id)
			);
		}
		$this->set('branches', $this->paginate());
		$this->set('cities', $this->Branch->City->find('all'));
		$this->set('city_id', $city_id);
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid branch', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('branch', $this->Branch->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->Branch->create();
			if ($this->Branch->save($this->data)) {
				$this->Session->setFlash(__('The branch has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The branch could not be saved. Please, try again.', true));
			}
		}
		$cities = $this->Branch->City->find('list');
		$this->set(compact('cities'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid branch', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Branch->save($this->data)) {
				$this->Session->setFlash(__('The branch has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The branch could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Branch->read(null, $id);
		}
		$cities = $this->Branch->City->find('list');
		$this->set(compact('cities'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for branch', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Branch->delete($id)) {
			$this->Session->setFlash(__('Branch deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Branch was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	
	function _filter_box()
	{
		$city_id = !empty($this->params['form']['filter_city']) ? $this->params['form']['filter_city'] : 0;
		$this->set('cities', $this->Branch->City->find('all'));
		$this->set('city_id', $city_id);
	}
	
	function manager_index() {
		$this->Branch->recursive = 0;
		$this->set('branches', $this->paginate());
	}

	function manager_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid branch', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('branch', $this->Branch->read(null, $id));
	}

	function manager_add() {
		if (!empty($this->data)) {
			
			$this->Branch->create();
			if ($this->Branch->save($this->data)) {
				$this->Session->setFlash(__('The branch has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The branch could not be saved. Please, try again.', true));
			}
		}
		$cities = $this->Branch->City->find('list');
		$this->set(compact('cities'));
	}

	function manager_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid branch', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Branch->save($this->data)) {
				$this->Session->setFlash(__('The branch has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The branch could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Branch->read(null, $id);
		}
		$cities = $this->Branch->City->find('list');
		$this->set(compact('cities'));
	}

	function manager_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for branch', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Branch->delete($id)) {
			$this->Session->setFlash(__('Branch deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Branch was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
